package amit.calc.form.table;

import java.awt.Color;
import java.awt.Component;
import java.text.NumberFormat;

import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

/**
 * 
 * @author ido
 *
 */
public class NumRenderer extends DefaultTableCellRenderer {
    NumberFormat formatter;
    
    public NumRenderer() { super(); }

    public void setValue(Object value) {
        if (formatter==null) {
            formatter = NumberFormat.getNumberInstance();
            formatter.setMaximumFractionDigits(2);
        }
        setText((value == null) ? "" : formatter.format(value));
    }
    
    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {
    	
    	if (Double.parseDouble(value.toString()) < 0) {
    		setBackground(Color.red);
    	}
    	else {
    		setBackground(Color.WHITE);
    	}
    	
    	super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    	setHorizontalAlignment(JLabel.RIGHT);
    	return this;
    	
    }
}